package cn.iocoder.yudao.module.forward.controller.admin.ack;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.forward.controller.admin.ack.vo.*;
import cn.iocoder.yudao.module.forward.dal.dataobject.ack.Ack1DO;
import cn.iocoder.yudao.module.forward.dal.dataobject.ack.Ack2DO;
import cn.iocoder.yudao.module.forward.service.ack.Ack1Service;

@Tag(name = "管理后台 - 应答信息")
@RestController
@RequestMapping("/forward/ack1")
@Validated
public class Ack1Controller {

    @Resource
    private Ack1Service ack1Service;

    @PostMapping("/create")
    @Operation(summary = "创建应答信息")
    @PreAuthorize("@ss.hasPermission('forward:ack1:create')")
    public CommonResult<Integer> createAck1(@Valid @RequestBody Ack1SaveReqVO createReqVO) {
        return success(ack1Service.createAck1(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新应答信息")
    @PreAuthorize("@ss.hasPermission('forward:ack1:update')")
    public CommonResult<Boolean> updateAck1(@Valid @RequestBody Ack1SaveReqVO updateReqVO) {
        ack1Service.updateAck1(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除应答信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('forward:ack1:delete')")
    public CommonResult<Boolean> deleteAck1(@RequestParam("id") Integer id) {
        ack1Service.deleteAck1(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得应答信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('forward:ack1:query')")
    public CommonResult<Ack1RespVO> getAck1(@RequestParam("id") Integer id) {
        Ack1DO ack1 = ack1Service.getAck1(id);
        return success(BeanUtils.toBean(ack1, Ack1RespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得应答信息分页")
    @PreAuthorize("@ss.hasPermission('forward:ack1:query')")
    public CommonResult<PageResult<Ack1RespVO>> getAck1Page(@Valid Ack1PageReqVO pageReqVO) {
        PageResult<Ack1DO> pageResult = ack1Service.getAck1Page(pageReqVO);
        return success(BeanUtils.toBean(pageResult, Ack1RespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出应答信息 Excel")
    @PreAuthorize("@ss.hasPermission('forward:ack1:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAck1Excel(@Valid Ack1PageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<Ack1DO> list = ack1Service.getAck1Page(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "应答信息.xls", "数据", Ack1RespVO.class,
                        BeanUtils.toBean(list, Ack1RespVO.class));
    }

    // ==================== 子表（应答统计） ====================

    @GetMapping("/ack2/list-by-ack1-id")
    @Operation(summary = "获得应答统计列表")
    @Parameter(name = "ack1Id", description = "所属应答")
    @PreAuthorize("@ss.hasPermission('forward:ack1:query')")
    public CommonResult<List<Ack2DO>> getAck2ListByAck1Id(@RequestParam("ack1Id") Integer ack1Id) {
        return success(ack1Service.getAck2ListByAck1Id(ack1Id));
    }

}